<?php

Class Pagination {
    
    //Settings
    private $base_url           = '';
    private $total_rows         =  0;
    private $per_page           = 10;
    private $uri_segment        =  3;
    private $perfix_segment     = false;
    private $destination        = true;
    
    //Vars
    private $current;
    private $total;
    
    function Pagination($params = array()) {
        
        if (count($params) > 0) {
            
            $this->initialize($params);     
        }
        
        log_message('debug', "Pagination Class Initialized");
    }
    
    
    public function initialize($params = array()) {
        
        if (count($params) > 0) {
            
            foreach ($params as $key => $val) {
                
                if (isset($this->$key)) {
                    
                    $this->$key = $val;
                }
            }
        }
    }
    
    //Вычисление всех необходимых параметров
    public function create($base_url = '', $total_rows = 0, $per_page = 10, $uri_segment = 5) {
        
        $CI =& get_instance();
        
        $this->base_url         = $base_url;
        $this->total_rows       = $total_rows;
        $this->per_page         = $per_page;
        $this->uri_segment      = $uri_segment;
        
        //Текущая страница
        if ($this->perfix_segment === false) {
        
            $this->current = $CI->uri->segment($this->uri_segment, 1);
        }
        else {
            
            $this->current = (int) substr($CI->uri->segment($this->uri_segment), strlen($this->perfix_segment));
        }
        
        //Всего страниц
        $this->total = ceil($this->total_rows/$this->per_page);
        
        if ($this->total <= 0) {
            
            $this->total = 1;
        }
        
        return $this;
    }
    
    //Стандартная навигация
    public function render($type = 'digg') {
        
        if ($this->total == 1) {
            
            return '';
        }
        
        $return = '<div class="pagination">';
        $CI =& get_instance();
        
        $lang_previous = $CI->lang->line('pagination_previous');
        $lang_next = $CI->lang->line('pagination_next');
        
        switch($type) {
            
            case 'digg':
                
                $prev = ($lang_previous) ? $this->previous($lang_previous) : $this->previous();
                $next = ($lang_next) ? $this->next($lang_next) : $this->next();
                
                $return .= ($prev == '') ? '' : '<span class="previous">'.$prev.'</span>';
                $return .= $this->numeric(8);
                $return .= ($next == '') ? '' : '<span class="next">'.$next.'</span>';
                break;
        }
        
        return $return.'</div>';
    }
    
    //Нумерация страниц
    public function numeric($limit, $view_last = 1, $delimiter = '...') {
        
        $return = '<ul>';
        
        $limit = ($limit > $this->total) ? $this->total : $limit;
        
        if ($this->current > round($limit/2)) {
            
            $start = $this->current - floor($limit/2);
            $end = $this->current + floor($limit/2);
            $end = ($end > $this->total) ? $this->total : $end;
        }
        else {
            
            $start = 1;
            $end = $limit;
        }
        
        for($i = $start; $i <= $end; $i++) {
            
            $current = ($i == $this->current) ? ' class="current"' : '';
            $return .= '<li'.$current.'>'.anchor($this->base_url.'/'.$this->perfix_segment.$i, $i).'</li>';
        }
        
        if ($view_last > 0 && $this->total > ($this->current + $view_last + 2)) {
            
            $return .= '<li>'.$delimiter.'</li>';
            
            for($i = ($this->total - $view_last + 1); $i <= $this->total; $i++) {
                
                $return .= '<li>'.anchor($this->base_url.'/'.$this->perfix_segment.$i, $i).'</li>';
            }
        }
        
        $return .= '</ul>';
        
        return $return;
    }
    
    //Ссылка на следующую страницу
    public function next($title = 'Next', $hide = true) {
        
        if ($hide && $this->current == $this->total) {
            
            return '';
        }
        
        $page = $this->current + 1;
        
        if ($page > $this->total) {
            
            return $title;
        }
        
        return anchor($this->base_url.'/'.$this->perfix_segment.$page, $title);
    }
    
    //Ссылка на предыдущую страницу
    public function previous($title = 'Previous', $hide = true) {
        
        if ($hide && $this->current == 1) {
            
            return '';
        }
        
        $page = $this->current - 1;
        
        if ($page < 1) {
            
            return $title;
        }
        
        return anchor($this->base_url.'/'.$this->perfix_segment.$page, $title);
    }
    
    //Ссылка на последнюю страницу
    public function last($title = 'Last', $hide = true) {
        
        if ($hide && $this->current == $this->total) {
            
            return '';
        }
        elseif ($this->current == $this->total) {
            
            return $title;
        }
        
        return anchor($this->base_url.'/'.$this->perfix_segment.$this->total, $title);
    }
    
    //Ссылка на первую страницу
    public function first($title = 'First', $hide = true) {
        
        if ($hide && $this->current == 1) {
            
            return '';
        }
        elseif ($this->current == 1) {
            
            return $title;
        }
        
        return anchor($this->base_url.'/'.$this->perfix_segment.'1', $title);
    }
    
    //Отображение текущей страницы и сколько всего
    public function pages($page = 'Page', $of = 'of') {
        
        return $page.' '.$this->current.' '.$of.' '.$this->total;
    }
    
    //Отображение показаных строк и сколько всего
    public function items($items = 'Items', $of = 'of') {
        
        $start = ($this->current - 1) * $this->per_page;
        $start = ($start == 0) ? 1 : $start;
        
        $end = $this->current * $this->per_page;
        $end = ($end > $this->total_rows) ? $this->total_rows : $end;
        
        return $items.' '.$start.' - '.$end.' '.$of.' '.$this->total_rows;
    }
    
    //Вычисление отступа в БД
    public function offset() {
        
        return ($this->current - 1) * $this->per_page;
    }
}